﻿using FormsAuthWeb.Models;
using FormsAuthWeb.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FormsAuthWeb
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [SubmitMethod(AutoRedirect = true)]
        public void Logout()
        {
            FormsAuthentication.SignOut();
        }

        public string GetUserMessage()
        {
            StringBuilder builder = new StringBuilder();
            if (Request.IsAuthenticated)
            {
                builder.AppendFormat("<h5 class='card-subtitle my-4'>当前用户已登录，登录名：{0}</h5>", HttpUtility.HtmlEncode(Context.User.Identity.Name));
                FormsPrincipal<UserInfo> user = Context.User as FormsPrincipal<UserInfo>;
                if (user != null)
                {
                    builder.AppendFormat("<p class='card-text'><strong>登录用户的自定义数据：</strong>{0}</p>", HttpUtility.HtmlEncode(user.UserData.ToString()));
                }
                builder.Append("<div class='button-group'><button class='btn btn-lg btn-success btn-width' type='submit' name='Logout'>退 出</button></div>");
            }
            else
            {
                builder.AppendFormat("<h6 class='card-subtitle my-3'>当前用户还未登录！</h6>");
            }
            return builder.ToString();
        }
    }
}